#include "userinterfaceZZL.h"
#include "chatinterface.h"
#include "displaydetailsinterface.h"

userinterfaceZZL::userinterfaceZZL(QString username, QWidget *parent)
    : QMainWindow{parent}
{
    this->username = username;
    this->centralWidget = new QWidget(this);
    this->usernameLabel = new QLabel("welcome");
    this->peerList = new QListWidget(centralWidget);
    this->groupList = new QListWidget(centralWidget);
    this->startChatButton = new QPushButton("开始聊天");
    this->displayDetailsButton = new QPushButton("查看详情");
    // this->clearSelectionButton = new QPushButton("清空选择");

    initData();
    initLayout();

    connect(startChatButton, SIGNAL(clicked()), this, SLOT(onStartChatButtonClicked()));
    connect(displayDetailsButton, SIGNAL(clicked(bool)), this, SLOT(onDisplayDetailsButtonClicked()));
}

void userinterfaceZZL::initData() {
    QStringList itemList1 = {"user1 (微信)", "user2 (QQ)", "user3 (QQ)"};
    this->peerList->addItems(itemList1);
    QStringList itemList2 = {"groupA", "groupB"};
    this->groupList->addItems(itemList2);

}

void userinterfaceZZL::initLayout() {
    this->resize(300, 300);
    this->centralWidget->setMinimumSize(400, 200);

    mainLayout = new QVBoxLayout(centralWidget);
    mainLayout->addWidget(usernameLabel);
    mainLayout->addWidget(peerList);
    mainLayout->addWidget(groupList);
    mainLayout->addWidget(startChatButton);
    mainLayout->addWidget(displayDetailsButton);

    this->peerList->setMinimumSize(50, 100);
    this->groupList->setMinimumSize(50, 100);

    this->setCentralWidget(centralWidget);
}

QString userinterfaceZZL::getWelcomeLabel() {
    return "Hi, " + username + ", welcome back to app!";
}

void userinterfaceZZL::onStartChatButtonClicked() {
    qDebug() << "start Chat";
    QList<QListWidgetItem*> groupSelected = this->groupList->selectedItems();
    QList<QListWidgetItem*> peerSelected = this->peerList->selectedItems();
    if (peerSelected.size() == 0 && groupSelected.size() == 0) {
        QMessageBox::warning(nullptr, "警告", "必须选择一个对象");
    } else if (peerSelected.size() == 0 || groupSelected.size() == 0) {
        QListWidgetItem* selected;
        if (groupSelected.size()) {
            selected = groupSelected[0];
        } else {
            selected = peerSelected[0];
        }
        qDebug() << "on" << selected->text();
        chatinterface* ld = new chatinterface(this->username, selected->text(), this);
        ld->show();
    } else {
        QMessageBox::warning(nullptr, "警告", "不能选择多个对象");
        for (int i = 0; i < groupList->count(); i++) {
            groupList->item(i)->setSelected(false);
        }
        for (int i = 0; i < peerList->count(); i++) {
            peerList->item(i)->setSelected(false);
        }

    }
}

void userinterfaceZZL::onDisplayDetailsButtonClicked() {
    QList<QListWidgetItem*> groupSelected = this->groupList->selectedItems();
    QList<QListWidgetItem*> peerSelected = this->peerList->selectedItems();
    if (peerSelected.size() == 0 && groupSelected.size() == 0) {
        QMessageBox::warning(nullptr, "警告", "必须选择一个对象");
    } else if (peerSelected.size() == 0 || groupSelected.size() == 0) {
        QListWidgetItem* selected;
        if (groupSelected.size()) {
            selected = groupSelected[0];
        } else {
            selected = peerSelected[0];
        }
        qDebug() << "on" << selected->text();
        // chatinterface* ld = new chatinterface(this->username, selected->text(), this);
        displaydetailsinterface *ld = new displaydetailsinterface(selected->text(), this);
        ld->show();
    } else {
        QMessageBox::warning(nullptr, "警告", "不能选择多个对象");
        for (int i = 0; i < groupList->count(); i++) {
            groupList->item(i)->setSelected(false);
        }
        for (int i = 0; i < peerList->count(); i++) {
            peerList->item(i)->setSelected(false);
        }

    }
}

QString userinterfaceZZL::getUserName() {
    return this->username;
}

